using System;
using System.Collections.Generic;
using System.Text;

namespace BuildingBasicDomain
{
    /// <summary>
    /// Represents a open or closed composite line where ILine Elements ec BeamElements or Edges .
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PolyLine<T>: List <T> where T : ILine , new ()
    {
        public static explicit operator PolyLine<T>(Area container)
        {
            return (PolyLine<T>)container.Polygon;
        }

        public bool IsIntersecedBy(ILine line)
        {
            bool isIntersected = false;

            foreach (ILine _line in this)
            {
                IPoint point = line.IntersectsLine(_line,CommonTypes.Geometry.Type_of_Intersection.On_Both );
                if (point != null)
                    isIntersected = true;
            }
            return isIntersected;
        }

        public IList<IPoint> PointsIntersects(ILine line)
        {
            List<IPoint> intersections = new List<IPoint>();
            foreach (ILine _line in this)
            {
                IPoint point = line.IntersectsLine(_line, CommonTypes.Geometry.Type_of_Intersection.On_Both);
                if (point != null)
                {
                    intersections.Add(point);
                }
            }
            return intersections;
        }

        public static explicit operator PolyLine<T>(Polygon polygon)
        {
            PolyLine<T> polyline = new PolyLine<T>();
            T linetype;

            for (int i = 0; i < polygon.Count - 1; i++)
            {
                linetype = new T(); ;

                linetype .SNode = polygon[i];
                linetype .ENode = polygon[i + 1];
                
                polyline.Add (linetype );
            }

            linetype = new T();
            linetype.SNode = polygon[polygon.Count - 1];
            linetype.ENode = polygon[0];

            polyline.Add(linetype);

            return polyline;
        }
    }
}
